package com.zxh.mall.order.config;

/**
 * @author zhengxh
 * @version 1.0
 * @date 2024/5/23 - 05 - 23 - 21:46
 * @description com.zxh.mall.order.config
 */

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.zxh.mall.order.vo.PayVO;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 支付宝的配置文件
 */
//@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 商户appid  沙箱账号 kymdri4832@sandbox.com
    public static String APPID = "2021000122668756";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCRliPKKmn53jR1ZsKvtT4g8dni0py0xOsSCSyB5SgwkhSi8VkRf/vgMs/vBwfSzjt1taAUnrvfNRv7tJkV2HF0lUKCNfU+ZMWbZx+cwcjw+hNiE0v2aig1MUWbNanimk//H4n3I7NQcpO2Rfrm3xWQMuodRayA9y762i33VR5kCVUUIn+5skcXxwawB81L57skixATcB3HTzJBHbFJ/4jkt05o3bnc5gJm8HMTOhlLhqQfw3/aRi9GUxbnkmdwNFT1GoZcdiN7LQG8+jRoBY1LBpCDSyVGN7f33YXsZ1NSMSG5IVwunqsdSjgvDutlrojUuTxk4SS18qCXbgHSR+RhAgMBAAECggEAKqdjavrMO9HXasIlrhca0cop/SOZl/jNqmYXrSL3iGt0Ml0lrFPj9sbMVwqnRyL3FsCU2xW6k2XbAf7ijxzv+Kk2aDHo3VPm9WXcRDAecEPEZAqlSdfGWfdKnnO1h1eQsn2KDWdfROWc011jajrX9XyFTPg51zsUf5hmuT0t/z6vYvdvqpXIfjJJVFPY6773gPeuo1xtC5YB/uJwk+r9kqgWa9c8MYF/ue5x05pyIFRuhS5AECuwuZ+XrLI7tZWNxecYbTHhZNPJ533vvbcGoWi++zEgVD9FnwlRXZi44lC1tbZzwwXeTR7XPw6NAI9vkqx81kc2E6vbN+wCWeTMBQKBgQDNE5lB5Vxu1CjdWU7LbW++wnEwrMlvem72NwgWPg1orbvTskjhuexG2tlFP2PkWsl8uSpXMOixJw2yd7wa4OzoplIrFn20/UcxqbfeQOBsX+aDh2rP44RKtjEgdVntkXiEgVq3+j5VI7HtAIV4CG2S7BXxPKQ4vpZjKR0WbcxIJwKBgQC1vNlBbdCOrntbNT34a+3zIZhkLh6uug5iZhm4Ijr7b0pk6lEPiYVcu6wl1zFkbyo0HKiU2a+cGmMX7ipDOrF3U+d1CpwD5rdrNceKMeGDoKTg65vqtMTCztcYbiGyWSwFaCUEUDFwzObpTulI7MzpWRI6R/t6/K5RMV6bxXL8NwKBgQCall1+uOCWgsNTJ9Ens5T6nSQp5vXpXMdsPkuQSLJe6CegCtK5pScgujPx6TjpmC4uIH50P2AIukGu+GcJc+fh8A4cgNW9qvcRzrfVh94kk532JAw2LXE5yGD78LobRP8uEzxJVw/JqiCjYTM7Kk8+40wfDvFT/QbsNL/Cen31HQKBgQCg7Qi0FzmGHpq8uPiW8XtSv3zhuLUSo035c89ybV0tJ+8ITTRaPbwJ0WUuXM8vz//fBUGCDQCjifoVRp/FyEM9Ft7tlOdxzOq91HFMvb6jf6yAPiFiQ4+3sNBmoZ7U/oEsiHu+xgQBjm8qSOXp3UXVmzm4weS9kuIIo17TMJQRIQKBgGtXxvjlBUAGrlf2s9pm6kfs0BLiZD21LwTas9JvH5ImYEch+PZIKfJ4qQhslKSL+uVEBBwap4LdM+2XSqH5r31YIFCsSVCjXnlFkqLJMrUeSE7tYEkt496V9+U2w49cVFTJzgO/qUpR5e+O8H9XqvPbMxbipOTzen58Abj/UtPA";
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://order.msb.com/payed/notify";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url = "http://order.msb.com/orderPay/returnUrl";
    // 请求网关地址
    public static String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqGW+4E46yhDstWpurqzHgKthSIdroeUbLyl1WZCiNmMesVSoJ7F4teJpcTRMuGJ7wznhi5DY85tA52zjGzhFircOcpH7NZD0Ff4B2dGnWobXTpl95otH4CbyRh121iYoUd+qupca8RUwcsCeiUIsK7tDQv0/zx1uvgBBX7FcuJTD80GPRTgYiJXZDuO885uO3ipFjGbN9uesnem+yeyvLo3ifnzOh3xO2lR1NYv03zWnaxTpjT1xejpeBiOaeAarhrIHvROH7xicM4z7xtxU9OyI4oCcYScftTtnJC+h7sxLFmWy83Ws8aZcUyHyu1cLVwsYIwJVMM1/xpKjoYP/HwIDAQAB";
    // 日志记录目录
    public static String log_path = "/log";
    // RSA2
    public static String SIGNTYPE = "RSA2";

    public String pay(PayVO payVO){
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL, APPID, RSA_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY,SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVO.getOut_trader_no());
        model.setSubject(payVO.getSubject());
        model.setTotalAmount(payVO.getTotal_amount());
        model.setBody(payVO.getBody());
        model.setTimeoutExpress("5000");
        model.setProductCode("11111");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
